﻿#region Disclaimer / License
// Copyright (C) 2011, Kenneth Bergeron, IAP Worldwide Services, Inc
// NOAA :: National Marine Fisheries Service
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
#endregion
using System;
using System.Windows.Forms;

namespace CatchProcessing
{
    /// <summary>
    /// A Print Preview for the Catch Report
    /// </summary>
    public partial class PrintForm : Form
    {
        /// <summary>
        /// The repory maker
        /// </summary>
        public PrintFish PrintFish { get; set; }
        /// <summary>
        /// A Print Preview for the Catch Report
        /// </summary>
        public PrintForm(DataGridView aFishGrid)
        {
            InitializeComponent();
            this.printDocument1.DefaultPageSettings.Landscape = true;
            PrintFish = new PrintFish(aFishGrid, this.printDocument1);
            this.printDocument1.EndPrint += new System.Drawing.Printing.PrintEventHandler(printDocument1_EndPrint);
        }
        /// <summary>
        /// Print is done event
        /// </summary>
        void printDocument1_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            EnableNavs();
        }
        /// <summary>
        /// Set up the havigation buttons
        /// </summary>
        private void EnableNavs()
        {
            this.PageToolStripTextBox.Text = (this.printPreviewControl1.StartPage+1).ToString();
            this.OfToolStripLabel.Text = "of " + this.PrintFish.NumberPages;
            this.PrevPageToolStripButton.Enabled = this.printPreviewControl1.StartPage > 0;
            this.NextPageToolStripButton.Enabled = this.printPreviewControl1.StartPage < this.PrintFish.NumberPages - 1;
        }
        /// <summary>
        /// Page Setup pressed
        /// </summary>
        private void SetupToolStripButton_Click(object sender, EventArgs e)
        {
            this.pageSetupDialog1.ShowDialog();
            this.printPreviewControl1.InvalidatePreview();
        }
        /// <summary>
        /// Save clicked (later)
        /// </summary>
        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
        }
        /// <summary>
        /// Print Pressed
        /// </summary>
        private void printToolStripButton_Click(object sender, EventArgs e)
        {
            if (this.printDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                this.printDocument1.Print();
        }
        /// <summary>
        /// 1st page pressed
        /// </summary>
        private void FirstPageToolStripButton_Click(object sender, EventArgs e)
        {
            this.printPreviewControl1.StartPage = 0;
            this.printPreviewControl1.InvalidatePreview();
            EnableNavs();
        }
        /// <summary>
        /// Last page pressed
        /// </summary>
        private void LastPageToolStripButton_Click(object sender, EventArgs e)
        {
            if (this.PrintFish.NumberPages > 0)
                this.printPreviewControl1.StartPage = this.PrintFish.NumberPages - 1;
            EnableNavs();
        }
        /// <summary>
        /// Next page pressed
        /// </summary>
        private void NextPageToolStripButton_Click(object sender, EventArgs e)
        {
            if (this.PrintFish.NumberPages > 0)
                this.printPreviewControl1.StartPage = (this.printPreviewControl1.StartPage + 1) % this.PrintFish.NumberPages;
            EnableNavs();
        }
        /// <summary>
        /// Previous page pressed
        /// </summary>
        private void PrevPageToolStripButton_Click(object sender, EventArgs e)
        {
            if (this.PrintFish.NumberPages > 0)
                this.printPreviewControl1.StartPage = (this.printPreviewControl1.StartPage - 1) % this.PrintFish.NumberPages;
            EnableNavs();
        }
        /// <summary>
        /// Zoom dropdown clicked
        /// </summary>
        private void ZoomToolStripDropDownButton_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.printPreviewControl1.Zoom = double.Parse(e.ClickedItem.Text.Replace("%", "")) / 100d;
        }
        /// <summary>
        /// Zoom drop opening, set it up
        /// </summary>
        private void ZoomToolStripDropDownButton_DropDownOpening(object sender, EventArgs e)
        {
            string pc = (this.printPreviewControl1.Zoom * 100d).ToString("N0") + "%";
            foreach (ToolStripMenuItem Item in this.ZoomToolStripDropDownButton.DropDownItems)
                Item.Checked = Item.Text == pc;
        }
        /// <summary>
        /// User is entering a page number
        /// </summary>
        private void PageToolStripTextBox_TextChanged(object sender, EventArgs e)
        {
            int Page = 0;
            if (!int.TryParse(this.PageToolStripTextBox.Text, out Page) || Page < 1 || Page > this.PrintFish.NumberPages)
            {
                System.Media.SystemSounds.Beep.Play();
            }
            else
            {
                this.printPreviewControl1.StartPage = Page - 1;
                EnableNavs();
            }
        }
    }
}
